<template>
  <div>
    <el-row style="margin: 18px 0px 0px 18px ">
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/admin' }">管理中心</el-breadcrumb-item>
        <el-breadcrumb-item>客房管家</el-breadcrumb-item>
        <el-breadcrumb-item>房间订退</el-breadcrumb-item>
      </el-breadcrumb>
    </el-row>
    <edit-form @onSubmit="loadRooms()" ref="edit"></edit-form>
    <el-card style="margin: 18px 2%;width: 95%">
      <el-table
        :data="rooms.slice((currentPage-1)*pagesize,currentPage*pagesize)"
        stripe
        style="width: 100%"
        :max-height="tableHeight">
        <el-table-column
          type="selection"
          width="55">
        </el-table-column>
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline>
              <el-form-item>
                <span>{{ props.row.roomIntroduction }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column
          prop="roomNumber"
          label="房号"
          width="150"
          fit>
        </el-table-column>
        <el-table-column
          prop="roomInformation"
          label="房间类型"
          width="150">
        </el-table-column>
        <el-table-column
          prop="name"
          label="姓名"
          width="100"
          fit>
        </el-table-column>
        <el-table-column
          prop="checkInTime"
          label="入住时间"
          width="120">
        </el-table-column>
        <el-table-column
          prop="checkOutTime"
          label="退房时间"
          fit>
        </el-table-column>
        <el-table-column
          prop="idCard"
          label="身份证号"
          show-overflow-tooltip
          fit>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="120">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="editBook(scope.row)"
              type="text"
              size="small">
              编辑
            </el-button>
            <el-button
              @click.native.prevent="deleteBook(scope.row.roomNumber)"
              type="text"
              size="small">
              退房
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row>
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="pagesize"
          :total="rooms.length">
        </el-pagination>
      </el-row>
      <div style="margin: 20px 0 20px 0;float: left">
        <el-button>取消选择</el-button>
        <el-button>批量删除</el-button>
      </div>
    </el-card>
  </div>
</template>

<script>
  import EditForm from './EditForm.vue'
  export default {
    name: 'BookManagement',
    components: {EditForm},
    data () {
      return {
        rooms: [],
        currentPage: 1,
        pagesize: 5
      }
    },
    mounted () {
      this.loadRooms()
    },
    computed: {
      tableHeight () {
        return window.innerHeight - 320
      }
    },
    methods: {
      handleCurrentChange: function (currentPage) {
        this.currentPage = currentPage
      },
      deleteBook (roomNumber) {
        this.$confirm('即将退房, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
            this.$axios
              .post('/deleteRoomStatus', {roomNumber: roomNumber}).then(resp => {
              if (resp && resp.code === 200) {
                this.loadRooms()
              }
            })
          }
        ).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      },
      editBook (item) {
        this.$refs.edit.dialogFormVisible = true;
        this.$refs.edit.updateRoomStatus = true;
        this.$refs.edit.form = {
          roomNumber: item.roomNumber,
          roomInformation: item.roomInformation,
          roomIntroduction: item.roomIntroduction,
          name: item.name,
          checkInTime: item.checkInTime,
          checkOutTime: item.checkOutTime,
          idCard: item.idCard
        }
      },
      loadRooms () {
        var _this = this;
        this.$axios.get('/allRoomStatus').then(resp => {
          if (resp && resp.code === 200) {
            console.log(resp.obj);
            _this.rooms = resp.obj;
          }
        })
      }
    }
  }
</script>

<style scoped>
</style>
